package cn.edu.jnu.basicms.service.impl;

import cn.edu.jnu.basicms.entity.User;
import cn.edu.jnu.basicms.mapper.UserMapper;
import cn.edu.jnu.basicms.service.UserService;
import cn.edu.jnu.basicms.utils.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    // 新增用户
    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    // 修改用户
    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    // 删除单个用户
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    // 批量删除用户（遍历调用单个删除）
    @Override
    public void deletebatchUser(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteUser(id);
        }
    }

    // 查询所有用户
    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    // 按ID查询单个用户
    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    // 按姓名查询用户
    @Override
    public List<User> selectByName(String name) {
        return userMapper.selectByName(name);
    }

    // 多条件查询
    @Override
    public List<User> selectByMore(String username, String name) {
        return userMapper.selectByMore(username, name);
    }

    // 分页模糊查询（封装分页逻辑）
    @Override
    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Page<User> page = new Page<>();
        Integer skipNum = (pageNum - 1) * pageSize;
        List<User> userList = userMapper.selectByPage(skipNum, pageSize, username, name);
        int total = userMapper.selectCount(username, name);
        page.setList(userList);
        page.setTotal(total);
        return page;
    }
}